package com.erp.produce.controller;

import com.erp.common.ResponseMessage;
import com.erp.common.controller.BaseController;
import com.erp.common.util.TimeUtil;
import com.erp.produce.entity.Customer;
import com.erp.produce.entity.Goods;
import com.erp.produce.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Api("原客户信息接口")
@RestController
@RequestMapping("/customer")
public class CustomerController implements BaseController<Customer> {

    @Autowired
    CustomerService customerService;


    @ApiOperation("新增客户实体信息")
    @PostMapping("/add")
    @Override
    public ResponseMessage add(Customer entity) {
        entity.setCreateTime(TimeUtil.now());
        if (entity.getCustomerName().isEmpty()) {
            return ResponseMessage.fail("客户公司名不能为空");
        }
        if (customerService.add(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("增加客户失败");
    }

    @ApiOperation("删除客户信息（支持多选）")
    @PostMapping("/list/delete")
    @Override
    public ResponseMessage deleteSome(@RequestParam(value = "idArray[]")Long[] ids) {
        if (customerService.deleteIds(ids)) {
            return ResponseMessage.ok();
        } else{
            return ResponseMessage.fail("删除失败，请重试");
        }
    }

    @ApiOperation("更新客户信息")
    @PostMapping("/update")
    @Override
    public ResponseMessage update(Customer entity) {
        if (customerService.update(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("更新客户信息失败");
    }

    @ApiOperation("分页展示客户信息")
    @GetMapping("/list/{page}/{size}")
    @Override
    public ResponseMessage listPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        List<Customer> customers = customerService.listPage(page, size);
        if (customers.size() == 0) {
            return ResponseMessage.fail("无相关客户信息");
        }
        return new ResponseMessage<>(customers);
    }

    @ApiOperation("分页展示客户信息（带模糊查询）")
    @GetMapping("/list/name/{page}/{size}")
    @Override
    public ResponseMessage listPageLike(@PathVariable("page") int page, @PathVariable("size")int size, String column) {
        List<Customer> customers = customerService.listPageLike(page, size, column);
        if (customers.size() == 0) {
            return ResponseMessage.fail("无相关客户信息");
        }
        return new ResponseMessage<>(customers);
    }

    @ApiOperation("返回所有客户信息")
    @GetMapping("/list")
    public ResponseMessage listOrderById(){
        List<Customer> customers = customerService.listOrderById();
        if (customers.size() > 0) {
            return new ResponseMessage(customers);
        } else{
            return ResponseMessage.fail("无客户信息");
        }
    }
}